/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihomeapp.ws;

import be.contribute.common.pojo.Temperature;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import java.util.List;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author wimvandecasteele
 */
public class HomeClient {

        private WebResource client;
        private String baseURL;
        
    public HomeClient(String pBaseUrl) {
                Client vClient = Client.create();
		baseURL = pBaseUrl;
                client = vClient.resource(baseURL);
    }
    
    public String getIntro()
    {
		ClientResponse response = client.accept(MediaType.TEXT_PLAIN)
                   .get(ClientResponse.class);
		if (response.getStatus() != 200) {
		   throw new RuntimeException("Failed : HTTP error code : "
			+ response.getStatus());
		}
		String output = response.getEntity(String.class); 
		return output;
    }
    
    public Temperature getRoomTemperature(int pRoomId)
    {
		Temperature temp = client.path("room")
                        .queryParam("id", pRoomId+"")
                                              .accept(MediaType.APPLICATION_JSON)
                                              .get(Temperature.class);
		return temp;
    }
    
    public List<Temperature> getRoomTemperatures(int pRoomId)
    {
		List<Temperature> temp = client.path("list")
                        .queryParam("id", pRoomId+"")
                                              .accept(MediaType.APPLICATION_JSON)
                                              .get(new GenericType<List<Temperature>>(){});
		return temp;
    }
    
    public boolean controlAirco(int pRoomId, int pState)
    {
		String response = client.path("airco")
                        .queryParam("id", pRoomId+"")
                        .queryParam("state", pState+"")
                        .get(String.class);
		return true;
    }
    
    public boolean controlHeater(int pRoomId, int pState)
    {
                String response =  client.path("heater")
                        .queryParam("id", pRoomId+"")
                        .queryParam("state", pState+"")
                        .get(String.class);
		return true;
    }
    
    public String controlCamera()
    {
        String response = client.path("cam")
                        .queryParam("type", "jpg")
                        .queryParam("name", "name1")
                        .get(String.class);
         return response;
    }
    
}
